package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.MessageTemplate;
import com.jumi.microservice.domain.vo.MessageTemplateVO;
import com.jumi.microservice.mapper.MessageTemplateMapper;
import com.jumi.microservice.service.MessageTemplateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class MessageTemplateServiceImpl extends ServiceImpl<MessageTemplateMapper,MessageTemplate> implements MessageTemplateService {
    @Resource
    private MessageTemplateMapper messageTemplateMapper;

    @Override
    public List<MessageTemplate> MessageTemplate() {
        LambdaQueryWrapper<MessageTemplate> goodsWrapper = Wrappers.lambdaQuery();
        goodsWrapper.eq(MessageTemplate::getIsDel, 0);
        goodsWrapper.orderByAsc(MessageTemplate::getId);
        List<MessageTemplate> messageTemplates = messageTemplateMapper.selectList(goodsWrapper);
        return messageTemplates;
    }

    @Override
    public void status(Long id, int status, String type, HttpServletRequest request) {
        QueryWrapper<MessageTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        MessageTemplate messageTemplate = baseMapper.selectOne(queryWrapper);
        //判断是否能查到信息
        ExceptionEnum.FAIL_NULL.doThrowIf(messageTemplate==null);
        String fieldName = "";
        switch (type){
            case "smtMessageSwitch":
                fieldName = "smt_message_switch";
                break;
            case "smtShortSwitch":
                fieldName = "smt_short_switch";
                break;
            case "smtWeixinSwitch":
                fieldName = "smt_weixin_switch";
                break;
            case "smtAppSwitch":
                fieldName = "smt_app_switch";
                break;
            case "smtAppletsSwitch":
                fieldName = "smt_applets_switch";
                break;
        }
        messageTemplateMapper.updateStatus(status,fieldName,id,request.getHeader(RequestConstant.ADMIN_NAME));
    }

    @Override
    public void update(MessageTemplateVO vo, HttpServletRequest request) {
        QueryWrapper<MessageTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",vo.getId());
        MessageTemplate message = baseMapper.selectOne(queryWrapper);
        ExceptionEnum.FAIL_NULL.doThrowIf(message==null);

        MessageTemplate messageTemplate = new MessageTemplate();
        messageTemplate.setId(vo.getId());
        messageTemplate.setSmtMessageSwitch(vo.getSmtMessageSwitch());
        messageTemplate.setSmtMessageContent(vo.getSmtMessageContent());
        messageTemplate.setSmtShortSwitch(vo.getSmtShortSwitch());
        messageTemplate.setSmtShortId(vo.getSmtShortId());
        messageTemplate.setSmtShortContent(vo.getSmtShortContent());
        messageTemplate.setSmtWeixinSwitch(vo.getSmtWeixinSwitch());
        messageTemplate.setSmtWeixinId(vo.getSmtWeixinId());
        messageTemplate.setSmtWeixinNumber(vo.getSmtWeixinNumber());
        messageTemplate.setSmtWeixinContent(vo.getSmtWeixinContent());
        messageTemplate.setSmtAppSwitch(vo.getSmtAppSwitch());
        messageTemplate.setSmtAppContent(vo.getSmtAppContent());
        messageTemplate.setSmtAppletsSwitch(vo.getSmtAppletsSwitch());
        messageTemplate.setSmtAppletsContent(vo.getSmtAppletsContent());
        messageTemplate.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        messageTemplateMapper.update(messageTemplate);
    }
}
